module(..., package.seeall)


local path = "images/game/"
local Sprite_Manager = require("sprite_manager")
function newNode(params)
	local nodeGroup = display.newGroup()
	local _W,_H = display.contentWidth,display.contentHeight
	nodeGroup.x = params.x
	nodeGroup.y = params.y
	local node = display.newImageRect(path..params.picture..".png",20,30)
--	local node = display.newText(params.text, 0, 0, native.systemFont, 30)
--	nodeGroup:insert(node, true);
	node.x = params.x
	node.y = params.y
	node.id = params.id
	node.name = "node"
	node.speed = params.speed
	local miss = display.newText("miss", 0, 0, native.systemFont, 30)
	miss:setTextColor(red)
	miss.alpha = 0
	Sprite_Manager.initSprite()
	local pow

--	nodeGroup:insert(pow);

	node.enterFrame = function(self,event)
		if self then
			if self.y > 300 then
				miss.x = node.x
				miss.y = node.y
				miss.alpha = 1
				self:destroy()
				transition.to( miss, { time = 1000, onComplete = hideText} )
				return
			end
			self.y = self.y + self.speed
		end
	end
	function hideText(obj)
		obj:removeSelf()
		obj = nil
	end
	function node:init()
		self:startListener()
	end

	function node:destroy()
		if self then
			self:stopListener()
			self:removeSelf()
			self = nil
		end
	end

	function node:isEaten()
		Runtime:removeEventListener("enterFrame", self)
		self.status = "eaten"
	end

	function node:startListener()
		Runtime:addEventListener("enterFrame", self)
		physics.addBody(self,"dynamic",{radius = 15})
		physics.isFixedRotation = true
	end

	function node:stopListener()
		Runtime:removeEventListener("enterFrame", self)
	end

	function node:runEffect()
		pow = sprite.newSprite(Sprite_Manager.getStatusSpriteSet())
		pow:prepare(Sprite_Manager.getStatusSprites().sequences[1].name)
		pow.x = node.x
		pow.y = node.y
		pow:play()
	end
	return node
end